/**
   WriteExcel
 * Time:2018年7月25日
 */
package com.ssm.util;


import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;







import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * @author 15199
 *
 */

public class WriteExcel {
	@SuppressWarnings("deprecation")
	public static void main(String[] args) throws ParseException {
		//1、创建工作簿
		Workbook wb = new XSSFWorkbook();

		//1.1、设置表格的格式----居中
		CellStyle cs = wb.createCellStyle();
		cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);



		//2.1、创建工作表
		Sheet sheet = wb.createSheet("学生表格");
		//2.2、合并单元格
		sheet.addMergedRegion(new CellRangeAddress(4, 8, 5, 9));
		//3.1、创建行----表头行
		Row row = sheet.createRow(0);
		//4、创建格
		Cell cell = row.createCell(0);
		cell.setCellValue("学号");
		cell.setCellStyle(cs);
		cell = row.createCell(1);
		cell.setCellValue("姓名");
		cell.setCellStyle(cs);
		cell = row.createCell(2);
		cell.setCellValue("时间");
		cell.setCellStyle(cs);


		String time="2000-5-20";

		SimpleDateFormat df =new SimpleDateFormat("yyyy-mm-dd");
		//5、写入实体数据

		User user1=new User("111","6666",df.parse(time));
		User user2=new User("123","hehe",df.parse(time));
		User user3=new User("456","haha",df.parse(time));

		List<User> list=new ArrayList<User>();

		list.add(user1);
		list.add(user2);
		list.add(user3);
		for (int i = 0; i < list.size(); i++) {
			//3.2、创建行----内容行
			row = sheet.createRow(i+1);
			User us = (User)list.get(i);
			//第几行第几格  第一行第一格为“code”
			row.createCell(0).setCellValue(us.getCode());
			row.createCell(1).setCellValue(us.getName());
			row.createCell(2).setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(us.getBirth()));
		}

		//6、将文件储存到指定位置
		try {
			FileOutputStream fout = new FileOutputStream("F:\\uuu.xlsx");
			wb.write(fout);
			fout.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}







